const loginForm = document.getElementById('loginForm');
const usernameInput = document.getElementById('username')
const pwdInput = document.getElementById('pwd')
import axios from '../../api/request'
import {saveToken,saveUser} from '../../utils/storageUtils'
loginForm.addEventListener('submit',async function(e){
    e.preventDefault()
    const username = usernameInput.value
    const pwd = pwdInput.value
    // console.log(username,pwd);
    // console.log(axios);
    
    const data = await axios({
        url:'/admin/login',
        method:'post',
        data:{
            username,
            pwd
        }
    })
    // console.log(data);
    
    if(data.code === 2000){
        saveToken(data.token)
        saveUser(data.data.user)
        window.location.href='/users.html'
    }else if(data.code === 2002){
        alert('用户名或密码错误')
    }else{
        alert('登录失败')
    }
})